/**
 * SpringMVC restful 风格
 * 协议控制器，控制关于协议的一些业务逻辑操作
 * @author yanwl
 * @date 2014-12-05
 */
package com.chnbst.bstyun.controller.mobile;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.DESUtil;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.eos.Eos;
import com.chnbst.bstyun.eos.UserOperator;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.pojo.extend.UserRegister;
import com.chnbst.bstyun.service.UserService;

@Controller
@RequestMapping("/mobile")
public class MobileUserController {
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private UserService userService;
	@Autowired
	private UserOperator userOperator;
	@Autowired
	private Eos eos;
	private static final String ROLE_PERSON = "ROLE_PERSON";//个人用户
	
	/**
	 * http://xxx.com/mobile/set_mobile
	 * get请求 返回手机注册页面
	 */
	@RequestMapping(value="/set_mobile",method=RequestMethod.GET)
	public ModelAndView setMobile(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("mobile/set_mobile");
		return mav;
	}
	
	/**
	 * http://xxx.com/mobile/set_authcode
	 * get请求 返回手机填写验证码页面
	 */
	@RequestMapping(value="/set_authcode",method=RequestMethod.GET)
	public ModelAndView setAuthcode(HttpServletRequest request){
		String userTel = request.getParameter("user_tel");
		String messageCode = (String)request.getSession().getAttribute("messageCode");
		System.out.println("验证码："+messageCode);
		ModelAndView mav = new ModelAndView();
		mav.addObject("userTel", userTel);
		mav.setViewName("mobile/set_authcode");
		return mav;
	}
	
	/**
	 * ajax 校验验证码输入的是否正确
	 * post请求 返回 boolean
	 */
	@RequestMapping(value="/check_authcode",method=RequestMethod.POST)
	@ResponseBody
	public boolean checkAuthcode(String code,HttpServletRequest request){
		boolean isTrue = false;
		String messageCode = (String)request.getSession().getAttribute("messageCode");
		if(code.equals(messageCode)) {
			isTrue = true;
		}
		return isTrue;
	}
	
	/**
	 * http://xxx.com/mobile/set_passwrod
	 * get请求 返回手机填写验证码页面
	 */
	@RequestMapping(value="/set_passwrod",method=RequestMethod.GET)
	public ModelAndView setPasswrod(HttpServletRequest request){
		String userTel = request.getParameter("user_tel");
		ModelAndView mav = new ModelAndView();
		mav.addObject("userTel", userTel);
		mav.setViewName("mobile/set_passwrod");
		return mav;
	}
	
	/**
	 * ajax 保存用户注册信息
	 * post请求 返回 boolean
	 * @throws Exception 
	 */
	@RequestMapping(value="/save_user",method=RequestMethod.POST)
	@ResponseBody
	public boolean saveUser(String userTel,String realName,String userPassword,HttpServletRequest request) throws Exception{
		/*商城用户注册*/
		User user = new User();
		String md5Password = DESUtil.encrypt(userPassword);
		user.setUserTel(userTel);
		user.setRealName(realName);
		user.setPhone(userTel);
		user.setUserPassword(md5Password);
		user.setUserValid("0");
		user.setLastLoginTime(new Date());
		user.setAccountMoney(0.00);
		user.setInvoiceMoney(0.00);
		user.setApprove("1");
		user.setUserMessageNum(0);
		user.setAddTime(new Date());
		user.setBoundTel(userTel);
		user.setIsLive("1");
		user.setIsYun("1");
		user.setRoleId(ROLE_PERSON);
		
		boolean success = false;
		try {
			String vn = eos.getVn();
			if(SignUtil.isNotEmpty(vn)) {
				user.setVirtualPhone(vn);
				userService.insertUser(user);
				
				//新增IP电话
				String ipStr = eos.addIp(vn, userPassword);
				if("1".equals(ipStr)) {
					/*即时通用户注册*/
					UserRegister uRegister = new UserRegister();
					uRegister.setLoginNumber(vn);
					uRegister.setLoginName(userTel);
					uRegister.setDisplayName(realName);
					uRegister.setPassword(userPassword);
					uRegister.setMd5(true);
					uRegister.setAllowMobileLogin(false);
					uRegister.setMobile(userTel);
					uRegister.setEnable(true);
					String result = userOperator.register(uRegister);
					if(!"OK".equals(result)) {
						//回滚删除用户
						userService.deleteUser(user);
						//回滚删除IP电话
						String ipRes = eos.deleteIp(vn);
						System.out.println("即时通用户注册错误信息："+result);
						System.out.println("删除IP电话信息："+ipRes);
					}else {
						success = true;
					}
				}else {
					success = false;
					//回滚删除用户
					userService.deleteUser(user);
				}
			}
		} catch (Exception e) {
			success = false;
			e.printStackTrace();
		}
		
		return success;
	}
}
